home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / IBTip / Source / Term.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  1.9 KB  |  112 lines

  1. #import "Term.h"
  2. #import <stdio.h>
  3. #import <stdlib.h>
  4. #import <string.h>
  5. #import <libc.h>
  6.  
  7. @implementation Term
  8.   
  9. + new
  10. {
  11.   self = [super newExistingfd:0];
  12.   stack = NULL;
  13.   crlast = NO;
  14.   return self;
  15. }
  16.  
  17. - pushState
  18. {
  19.   struct stateStack * p;
  20.   p = (struct stateStack *)malloc(sizeof(struct stateStack));
  21.   p->next = stack;
  22.   p->state = currentState;
  23.   stack = p;
  24.   return self;
  25. }
  26.  
  27. - popState
  28. {
  29.   struct stateStack *p;
  30.   if (stack==NULL) {
  31.     fprintf(stderr,"Term.m error: stack empty on popState\n");
  32.   }
  33.   currentState = stack->state;
  34.   p = stack;
  35.   stack = stack->next;
  36.   free(p);
  37.   return [self activateState];
  38. }
  39.  
  40. - (int)getchar
  41. {
  42.   char ch;
  43.   int cc;
  44.   [[[self pushState] setRaw] setCrmod];
  45.   cc=read(fd, &ch, 1);
  46.   [self popState];
  47.   if (cc==1) {
  48.     crlast = (ch=='\r' || ch=='\n');
  49.     return ch;
  50.   }
  51.   else
  52.     return EOF;
  53. }
  54.  
  55. - (int)getcharWithPrompt:(char *)prompt
  56. {
  57.   [self putPrompt:prompt];
  58.   return [self getchar];
  59. }
  60.  
  61. - (int)getline:(char *)string size:(int)max
  62. {
  63.   char ch;
  64.   int c,i;
  65.   i=0;
  66.   [[[[self pushState] unSetRaw] setCrmod] setEcho];
  67.   while (--max>0 && (c=read(fd,&ch,1))==1 && ch!='\n')
  68.     string[i++]=ch;
  69.   if (ch=='\n')
  70.     string[i++]=ch;
  71.   string[i]=0;
  72.   [self popState];
  73.   crlast=YES;
  74.   return i;
  75. }
  76.  
  77. - (int)getline:(char *)string size:(int)max WithPrompt:(char *)prompt
  78. {
  79.   [self putPrompt:prompt];
  80.   return [self getline:string size:max];
  81. }
  82.  
  83. - putString:(char *)string
  84. {
  85.   [[[self pushState] unSetRaw] setCrmod];
  86.   if (crlast && *string=='\n')  // don't skip line at beginning of string
  87.     [self writeOut:string+1];
  88.   else
  89.     [self writeOut:string];
  90.   [self popState];
  91.   crlast = (string[strlen(string)-1]=='\n');
  92.   return self;
  93. }
  94.  
  95. - putPrompt:(char *)string
  96. {
  97.   [[[self pushState] unSetRaw] setCrmod];
  98.   printf("%s",string);
  99.   fflush(stdout);
  100.   [self popState];
  101.   return self;
  102. }
  103.  
  104. - ringBell
  105. {
  106.   char bell = '\a';
  107.   write (fd, &bell, 1);
  108.   return self;
  109. }
  110.  
  111. @end
  112.